
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Pattern;

public class sortearCalendario extends SeleneseTestCase {
	
	private Connection connect = null;
	private Statement statement = null;
	private PreparedStatement preparedStatement = null;
	private ResultSet resultSet = null;
	
	@Before
	public void setUp() throws Exception {
		selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://localhost:8081/");
		selenium.start();
	}

	@Test
	public void testSortearCalendario() throws Exception {
		selenium.open("/Miscore/ingresar.php");
		selenium.type("name=usuario", "prueba");
		selenium.type("name=pass", "1qazxsw2");
		selenium.click("css=input.btn_ingresar");
		selenium.waitForPageToLoad("30000");
		selenium.click("link=Calendario");
		selenium.waitForPageToLoad("30000");
		selenium.select("name=seleccionado", "label=prueba2");
		selenium.click("name=sortear");
		selenium.waitForPageToLoad("30000");
		verifyTrue(selenium.isTextPresent("Calendario Sorteado"));
		verifyTrue(consulta());
		
	}

	public boolean consulta() throws Exception {
		 Connection conn = null;
		  String url = "jdbc:mysql://localhost:3306/";
		  String dbName = "mydb";
		  String driver = "com.mysql.jdbc.Driver";
		  String userName = "root"; 
		  String password = "";
		  try {
			  Class.forName(driver).newInstance();
		  conn = DriverManager.getConnection(url+dbName,userName,password);
		  statement=conn.createStatement();
		  resultSet = statement.executeQuery("SELECT estado FROM torneo WHERE nombre_torneo = 'prueba2'");
		  comparar(resultSet);
		  System.out.println("Connected to the database");
		  conn.close();
		  System.out.println("Disconnected from database");
		  return true;
		  } catch (Exception e) {
		  e.printStackTrace();
		  return false;
		  }
}

	private void comparar(ResultSet resultSet) throws SQLException {
	
		while (resultSet.next()) {
			int estado = resultSet.getInt("estado");
			assertTrue(estado==0);
		}
	}
	
	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}
}
